<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
    <script type="text/javascript" src="https://cdn.bootcss.com/jsencrypt/3.0.0-rc.1/jsencrypt.min.js"></script>

    <script>
        $(document).ready(function(){
            //页面加载完毕后 查看 是否有公钥，有的话显示在界面上
            window.localStorage.removeItem("pubKey")
            const publicKey=window.localStorage.getItem("pubKey");
            if(publicKey!==null)
            {
                $("#txtKey").val(publicKey)
            }

        })

    </script>
</head>
<body>

<script>

    const getPublicKey=()=>{
        fetch("http://118.31.52.216:8090/token/pubkey",{method:"POST"})
            .then(function(result){
                result.json().then(function(data){
                    const publicKey=data.pubkey;
                    //保存至localStorage
                    window.localStorage.setItem("pubKey",publicKey)

                    $("#txtKey").val(publicKey)

                })
            })

    }
    const login=()=>{
        const publicKey=window.localStorage.getItem("pubKey");


        if(publicKey===null)
        {
            alert("请先获取公钥")
        }
        else {
            const encrypt = new JSEncrypt();
            encrypt.setPublicKey(publicKey);

            let userName=$("#txtUserName").val();
            let userPwd=$("#txtUserPwd").val();

            userName=encrypt.encrypt(userName); //利用公钥加密了
            userPwd=encrypt.encrypt(userPwd );

            const url="http://118.31.52.216:8090/token/access_token?uname="+encodeURIComponent(userName)+"&usec="+encodeURIComponent(userPwd);
            self.location=url;
        }


    }

</script>
<div>
    用户名:<input type="text" id="txtUserName" autocomplete="off"/>
</div>
<div>
    密码:<input type="text" id="txtUserPwd" autocomplete="off"/>
</div>
<input type="button" value="获取公钥" onclick="getPublicKey()"/>

<input type="button" value="加密后跳转到token地址" onclick="login()"/>
<hr/>
公钥<br/>
<textarea autocomplete="off" style="width:300px;height: 200px" id="txtKey"></textarea>
<br/>

</body>
</html>
